//- Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
//- <https://www.chatopera.com>, Licensed under the Chunsong Public 
//- License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
//- Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
//- Licensed under the Apache License, Version 2.0
//- http://www.apache.org/licenses/LICENSE-2.0
.uk-layui-form
    form.layui-form.uk-form(action='/admin/role/auth/save.html')
        input(type='hidden', name='id', value=role.id)
        - var roleAuthString=[]
        if roleAuthList
            for i, index in roleAuthList
                if index>0
                    - roleAuthString.push(i.dicid)
        input#menus(type='hidden', name='menus', value=roleAuthString)
        .layui-form-item
            .layui-input-inline
                ul#roleTree.ztree
        .layui-form-button
            .layui-button-block
                if user.superadmin
                    button.layui-btn(lay-submit, lay-filter='formDemo') 立即提交
                    button#reset.layui-btn.layui-btn-original(type='reset', href='/admin/role/auth.html?id=${role.id}') 重置
                else
                    button.layui-btn.layui-btn-original(type='reset', onclick='layer.close(top.layerwin);') 关闭
script(type='text/javascript').
    var setting = {
        check: {enable: true},
        data: {simpleData: {enable: true}},
        callback: {onCheck: zTreeOnCheck}
    };
    //获取所有选中节点的值
    function zTreeOnCheck() {
        var treeObj = $.fn.zTree.getZTreeObj("roleTree");
        var nodes = treeObj.getCheckedNodes(true);
        var msg = "";
        for (var i = 0; i < nodes.length; i++) {
            if (msg != '' && nodes[i].id != '') {
                msg = msg + ",";
            }
            msg += nodes[i].id;
        }
        $("#menus").val(msg);
    }
    var _resourceList = !{pugHelper.toJSON(resourceList)}
    var _roleAuthList = !{pugHelper.toJSON(roleAuthList)}
    $.each(_resourceList, function (i, r) {
        r.id = r.id;
        r.pId = r.parentid;
        $.each(_roleAuthList, function (u, o) {
            if (o.dicid == r.id) {
                r.checked = true
            }
        })
        r.name = r.name;
        r.value = r.code;
        r.open = true;
        r.icon = r.level && (r.level == '1' || r.level == '2') ? '/images/dir.png' : '/images/menu.png';
    });
    _resourceList.unshift(
        {
            id: "#{sysDic.id ? sysDic.id : ''}",
            pId: '0',
            name: "菜单资源",
            open: true,
            value: ""
        }
    )
    var zNodes = _resourceList;
    $(document).ready(function () {
        $.fn.zTree.init($("#roleTree"), setting, zNodes);
    });
    $("#reset").on('click', function () {
        $.fn.zTree.init($("#roleTree"), setting, zNodes);
    })
            
